package dyyx;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.jedis.Tuple;

public class RedisClientShardTest {
	
	private static volatile ShardedJedisPool sharedPool;	
	
	
	public synchronized static ShardedJedisPool getShardedJedisPool(){
		if(sharedPool!=null){
			return sharedPool;
		}
		JedisPoolConfig config =new JedisPoolConfig();
		config.setTestOnBorrow(true);
		
		List<JedisShardInfo> shardInfoList =new ArrayList<JedisShardInfo>();
        // JedisShardInfo infoA = new JedisShardInfo("127.0.0.1", 6379);
        // JedisShardInfo infoB = new JedisShardInfo("127.0.0.1", 6380);
		// 设置 name , 构造一致性hash环时会使用 name 
		JedisShardInfo infoA = new JedisShardInfo("127.0.0.1", 6379,"127.0.0.1:6379");
        JedisShardInfo infoB = new JedisShardInfo("127.0.0.1", 6380,"127.0.0.1:6380");
		
        shardInfoList.add(infoA);
        shardInfoList.add(infoB);
		 
		ShardedJedisPool poolTmp = new  ShardedJedisPool(config,shardInfoList);
		sharedPool = poolTmp;
		return sharedPool;		
	}
	
	
	
	
	
	public static void main(String[] args) throws Exception {
		
		System.out.println("hello,RedisClientShardTest");
		ShardedJedisPool pool = getShardedJedisPool();
		System.out.println("pool="+pool);
		

		ShardedJedis jedis = null;
		
		
		jedis = pool.getResource();
		
		final int NUM = 10;
		
		for(int i=0;i<NUM;i++){
			String key = "client_shard_test_key_"+i;
			String value = key+"_value_"+new Date();
			jedis.set(key, value);
		}
		
		for(int i=0;i<NUM;i++){
			String key = "client_shard_test_key_"+i;
			String value = jedis.get(key);
			System.out.println("key="+key+",value="+value);
		}
		
		for(int i=0;i<NUM;i++){
			String key = "client_shard_test_key_"+i;	
            System.out.println("key="+key+",shardInfo="+jedis.getShardInfo(key));
		}
		
		
		
		
		String key = "listkey";
		jedis.del(key);
		//  head (LPUSH) or tail (RPUSH)
		jedis.rpush(key, "a","b");
		jedis.lpush(key, "c","d");
		
		List<String> list = jedis.lrange(key, 0, 100);
		// [d, c, a, b]
		System.out.println(list);
		
		list = jedis.lrange(key, 0, 2);
		// [d, c, a]
		System.out.println(list);

		
		//String str = jedis.get(key);
		
		// System.out.println(str);
		
		// jedis.zremrangeByScore(key, min, max)

		// jedis.zremrangeByRank(key, start, stop);

		key = "zsetkey";
		Date time = new Date();
		int size = 5;
		double score = 0 - time.getTime();
		jedis.zadd(key, score, time+"");
		
		Long zcard = jedis.zcard(key);
		System.out.println("zcard="+zcard);

		Long zcount = jedis.zcount(key,  Long.MIN_VALUE, Long.MAX_VALUE);
		System.out.println("zcount="+zcount);

		
		Set<Tuple> zsets = jedis.zrangeByScoreWithScores(key,  Long.MIN_VALUE, Long.MAX_VALUE,0,size);
		
		System.out.println(zsets);
		
		int start = size;
		int stop = Integer.MAX_VALUE;
		jedis.zremrangeByRank(key, start, stop);
		
		
		System.out.println("*******");
		
		key = "zsetkey2";
		score = time.getTime();
		jedis.zadd(key, score, time+"");
		
		zcard = jedis.zcard(key);
		System.out.println("zcard="+zcard);

		zcount = jedis.zcount(key,  0, Long.MAX_VALUE);
		System.out.println("zcount="+zcount);

		
		zsets = jedis.zrangeByScoreWithScores(key,  0, Long.MAX_VALUE,0,size);
		
		System.out.println(zsets);
		
		start = 0 - size - 9;
		stop = 0 - size;
		jedis.zremrangeByRank(key, start, stop);
		
		
			
		jedis.close();
		
		
		
		
		
		
		

	}
	
	
}
